<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.7.6/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" >
    <div class="layui-form-item">
        <label class="layui-form-label required">编号</label>
        <div class="layui-input-block">
            <input type="number" name="serial_num" lay-verify="required" lay-reqtext="编号" placeholder="" value="" class="layui-input">
            <tip>账本编号</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">花束名</label>
        <div class="layui-input-block">
            <input type="hidden" name="flower_id"/>
            <input type="text" name="flower_name" placeholder="" value="" class="layui-input" id="demo3">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">花束图片</label>
        <div class="layui-input-block">
            <input type="hidden" name="img_id" id="productImg"/>
            <button type="button" class="layui-btn" id="productImgButton">上传图片</button>
            <button type="button" class="layui-btn layui-btn-warm" id="picview">从图库选择</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="productImgImg" style="width: 50%;">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">微信昵称</label>
        <div class="layui-input-block">
            <input type="text" name="vx" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">收花人手机号</label>
        <div class="layui-input-block">
            <input type="number" name="mobile" lay-verify="" lay-reqtext="手机不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">收花人姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="" lay-reqtext="收花人姓名不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">收花人地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="" lay-reqtext="收花人地址不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">配送时间</label>
        <div class="layui-input-inline">
            <input type="text"  id="test1" lay-verify="date" placeholder="" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <input type="text"  class="layui-input" id="test2" placeholder=" - ">
        </div>
        <input type="hidden" name="date" class="layui-input">
        <input type="hidden" name="time" class="layui-input" value="00:00-23:59">
        <input type="hidden" name="send_time" class="layui-input">
        <input type="hidden" name="start_time" class="layui-input">
        <input type="hidden" name="end_time" class="layui-input">
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">订单类型</label>
        <div class="layui-input-block">
            <input type="radio" name="order_type" value="0" title="微信" checked="">
            <input type="radio" name="order_type" value="1" title="门店">
            <input type="radio" name="order_type" value="2" title="美团">
            <input type="radio" name="order_type" value="3" title="饿了么">
            <input type="radio" name="order_type" value="4" title="抖音">
            <input type="radio" name="order_type" value="5" title="高德">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">配送类型</label>
        <div class="layui-input-block">
            <input type="radio" name="send_type" value="0" title="自取" lay-filter="send_type" checked="">
            <input type="radio" name="send_type" value="1" title="自配" lay-filter="send_type">
            <input type="radio" name="send_type" value="2" title="达达" lay-filter="send_type">
            <input type="radio" name="send_type" value="3" title="专车" lay-filter="send_type">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">配送费</label>
        <div class="layui-input-block">
            <input type="number" name="send_price"  placeholder="" value="0" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">订单价格</label>
        <div class="layui-input-block">
            <input type="number" name="order_price" lay-verify="required" lay-reqtext="订单价格不能为空" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">线下核销</label>
        <div class="layui-input-block">
            <input type="radio" name="offline_verify" value="0" title="否" checked="">
            <input type="radio" name="offline_verify" value="1" title="是">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡片</label>
        <div class="layui-input-block">
            <input type="radio" name="card" value="0" title="无" checked="">
            <input type="radio" name="card" value="1" title="有">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡片内容</label>
        <div class="layui-input-block">
            <input type="text" name="card_content" lay-verify="" lay-reqtext="" placeholder="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">节日分类</label>
        <div class="layui-input-block">
            <select name="holiday_type" id="holiday_type" lay-verify="required">
                <option value=""></option>
                <option value="0">其他</option>
                <option value="1">情人节</option>
                <option value="2">七夕</option>
                <option value="3">520</option>
                <option value="4">妇女节</option>
                <option value="5">教师节</option>
                <option value="6">母亲节</option>
                <option value="7">圣诞节</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">订单状态</label>
        <div class="layui-input-block">
            <input type="radio" name="order_status" value="0" title="未处理" checked="">
            <input type="radio" name="order_status" value="1" title="待配送">
            <input type="radio" name="order_status" value="2" title="已完成">
            <input type="radio" name="order_status" value="3" title="退款">
        </div>
    </div>
<!--    <div class="layui-form-item layui-form-text">-->
<!--        <label class="layui-form-label">备注信息</label>-->
<!--        <div class="layui-input-block">-->
<!--            <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>-->
<!--        </div>-->
<!--    </div>-->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" disabled lay-filter="statusDemo" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script src="/static/lib/layui-v2.7.6/layui.js" charset="utf-8"></script>
<script src="/static/js/common.js" charset="utf-8"></script>
<script src="/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','laydate','tableSelect','easyCropper'], function () {
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            easyCropper = layui.easyCropper,
            tableSelect = layui.tableSelect,
            $ = layui.$;
        laydate.render({
            elem: '#test1',
            value: new Date()
            ,done: function(value, date, endDate){
                $("input[name='date']").val(value)
                var send_time = value +' '+ $("#test2").val()
                $("input[name='send_time']").val(send_time.trim())
                var timeArr =  $("input[name='time']").val().split('-')
                $("input[name='start_time']").val(value+' '+timeArr[0].trim());
                $("input[name='end_time']").val(value+' '+timeArr[1].trim())
                console.log(value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
            }
        });
        //日期时间范围
        laydate.render({
            elem: '#test2'
            ,type: 'time'
            ,format: 'HH:mm' //可任意组合
            ,range: true
            ,done: function(value, date, endDate){
                var timeArr = value.split('-');
                var timeStr = timeArr[0].trim()+'-'+timeArr[1].trim();
                $("input[name='time']").val(timeStr)
                var date_time = $("input[name='date']").val()
                $("input[name='send_time']").val(date_time +' '+timeStr)
                console.log(value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
                $("input[name='start_time']").val(date_time+' '+timeArr[0].trim());
                $("input[name='end_time']").val(date_time+' '+timeArr[1].trim())

            }
        });
        $("input[name='date']").val(getNowFormatDate())
        $("input[name='send_time']").val(getNowFormatDate())
        $("input[name='start_time']").val(getNowFormatDate()+' 00:00')
        $("input[name='end_time']").val(getNowFormatDate()+' 23:59')

        //多条件搜索
        tableSelect.render({
            elem: '#demo3',
            checkedKey: 'id',
            searchType: 'more', //开启多条件搜索
            searchList: [
                {searchKey: 'serial_num', searchPlaceholder: '搜索花束编号'}, //搜索条件1
                {searchKey: 'name', searchPlaceholder: '搜索花束名'}, //搜索条件2
                {searchKey: 'year', searchPlaceholder: '年份',select:{2022:'2022',2023:'2023',2024:'2024',2025:'2025',2026:'2026',2027:'2027',2028:'2028',2029:'2029',2030:'2030',2031:'2031',2032:'2032'}}, //搜索条件3
                {searchKey: 'holiday_type', searchPlaceholder: '搜索花束分类',select:{0:'其他',1:'情人节',2:'七夕',3:'520',4:'妇女节',5:'教师节',6:'母亲节',7:'圣诞节'}},  //搜索条件2 0.其他 1.情人节 2.七夕 3.520 4.妇女节 5.教师节 6.母亲节 7.圣诞节
            ],
            table: {
                url: '/huashu/index',
                cols: [[
                    { type: 'radio' },
                    // { field: 'id', title: 'ID' },
                    {field: 'serial_num', title: '编号',sort: true,align:'center'},
                    {field: 'img',  title: '图片',align:'center', templet:function (d) {
                            return '<img  src="/img'+ (d.img.file_path == "/" ? "":d.img.file_path) + '/' +d.img.file_name +'" onclick="showimg(this)" id="driimg" style="display:inline-block;width: 20px;height: 40px;"/>';
                        }},
                    {field: 'name', title: '名称',align:'center'},
                    {field: 'status', title:'状态',  sort: true,templet: '#switchTpl', unresize: true},
                    {field: 'price',  title: '价格',sort: true,align:'center'},
                ]]
            },
            done: function (elem, data) {
                $("input[name='flower_id']").val(data.data[0].id)
                $("input[name='flower_name']").val(data.data[0].name)
                $("input[name='img_id']").val(data.data[0].img.id)
                $("input[name='order_price']").val(data.data[0].price)
                $("#productImgImg").attr('src','/img'+ (data.data[0].img.file_path == '/' ? '' : data.data[0].img.file_path) + '/' +data.data[0].img.file_name );
                $("#holiday_type").find("option[value="+data.data[0].holiday_type+"]").attr("selected",true)
                form.render('select');
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            }
        });
        //渲染下拉
        $.ajax({
            url: "/config/getConfigByKey",
            type: 'post',
            data:{"key":"holiday_type"},
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            async: false,
            success: function (ret) {
                $("#holiday_type").val(ret.data)
                form.render('select');
            },
            error: function (xhr, textstatus, thrown) {
                layer.msg('操作失败');
            }
        });
        //创建一个图片裁剪上传组件
        var productImgCropper = easyCropper.render({
            elem: '#productImgButton'
            // ,defaultImg: '../static/images/noimg.jpg' // 默认图片 选填
            ,size: 20480    // 大小限制 默认1024k 选填
            ,saveW: 480    //保存宽度
            ,saveH: 600     //保存高度
            // ,mark:1/1   //选取比例
            ,fileType:["image/jpg", "image/png", "image/jpeg"]  //允许上传的图片类型
            ,area:'900px'  //弹窗宽度
            ,url: '/image/upload'  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样回的JOSN一样
            // ,choose: function(obj){}
            ,done: function(data){ //上传完毕回调
                $("#productImg").val(data.id);
                $("#productImgImg").attr('src',data.url);
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: "/order/add",
                type: 'post',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                data:JSON.stringify({
                    "address": data.field.address,
                    "card": parseInt(data.field.card),
                    "flower_name": data.field.flower_name,
                    "img_id": parseInt(data.field.img_id),
                    "flower_id": parseInt(data.field.flower_id),
                    "user_id": parseInt(data.field.user_id),
                    "mobile": data.field.mobile,
                    "name": data.field.name,
                    "offline_verify": parseInt(data.field.offline_verify),
                    "order_price": parseInt(data.field.order_price),
                    "send_price": parseInt(data.field.send_price),
                    "order_status": parseInt(data.field.order_status),
                    "order_type": parseInt(data.field.order_type),
                    "card_content": data.field.card_content,
                    "send_type": parseInt(data.field.send_type),
                    "serial_num": parseInt(data.field.serial_num),
                    "start_time": data.field.start_time,
                    "end_time": data.field.end_time,
                    "send_time": data.field.send_time,
                    "vx": data.field.vx,
                    "holiday_type": parseInt(data.field.holiday_type),
                  }),
                async: false,
                success: function (ret) {
                    var msg = ret.code > 0  ? ret.msg : "保存成功" ;
                    layer.msg(msg, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        parent.layui.table.reload('currentTableId',{page:{curr:1}})
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                error: function (xhr, textstatus, thrown) {
                    layer.msg('保存失败');
                }
            });
            return false;
        });
        //弹层选择
        $(document).on('click','#picview',function(res){
            let name = $(this).data('name');
            layui.data('_fm', {key: '_picview_name',value: name});
            layer.open({
                type:2
                , area: ['620px', '600px']
                ,content: ['/static/page/img/pop.html', 'no']
            })
        });
        //弹层选择后的回调
        window.callback = function(res){
            // let name = layui.data('_fm')._picview_name;
            console.log(res)
            $("input[name='img_id']").val(res.id)
            $("#productImgImg").attr('src',res.thumb);
            layui.data('_fm',null);
        }
    });
</script>
</body>
</html>